<?php

namespace pay\wxNotify;

use pay\wxpay\lib\WxPayApi;

class wxNotify extends \pay\wxpay\lib\WxPayNotify
{
    public function NotifyProcess($data, &$msg)
    {
//        $data = $this->data;
        if ($data['result_code'] == 'SUCCESS') {
            $orderNo = $data['out_trade_no'];
            Db::startTrans();
            try {
                $order = Db::name('order')->where('order_no','=',$orderNo)->lock(true)->find();
                if ($order['status'] == 0) {

                    return true;
                }
                Db::commit();
            } catch (Exception $ex) {
                Db::rollback();
                Log::error($ex);
                // 如果出现异常，向微信返回false，请求重新发送通知
                return false;
            }
        }
        return true;
    }

    private function updateOrderStatus($orderID, $success)
    {
        $status = $success ? OrderStatusEnum::PAID : OrderStatusEnum::PAID_BUT_OUT_OF;
        $order = Db::name('order')->where('order_id',$orderID)->update(['status'=>$status]);
    }
}
